package com.zhdl.network.service.impl;

import cn.hutool.core.util.HexUtil;
import com.google.common.eventbus.Subscribe;
import com.project.command.faq.event.behavior.BehaviorPerform;
import com.project.command.instruction.bo.SingleInstruction;
import com.project.command.instruction.strategy.process.data.AfterStrategyParam;
import com.project.common.util.ByteHelper;
import com.zhdl.common.eventbus.center.EventBusCenter;
import com.zhdl.common.util.IdUtil;
import com.zhdl.modules.tools.CommonCallback;
import com.zhdl.network.modbusrtu.after.ModbusAfterService;
import com.zhdl.network.modbusrtu.analysis.ModbusAnalysis;
import com.zhdl.network.modbusrtu.dto.ModbusDto;
import com.zhdl.network.service.BatteryRS485;
import com.zhdl.network.service.conf.PortConf;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static com.project.command.instruction.strategy.abs.AbsDic.INSTRUCTION_DEFAULT_HEX;

@Service
@Slf4j
public class BatteryRS485Impl implements BatteryRS485 {
    public Map<String, CommonCallback<String>> commonCallbackMap = new ConcurrentHashMap<>();
    //    private String readCmd = "01 03 00 01 00 2A 95 D5";
//    private String readCmd = "01 03 00 21 00 01 D4 00";
//    private String readCmd = "01 03 00 14 00 01 C4 0E";
    private String readCmd = "01 03 00 13 00 02 35 CE";
    @Resource
    private PortConf portConf;
    @Resource
    private BehaviorPerform behaviorPerform;

    public void init() {
        EventBusCenter.getEventBus().register(this);
    }

    @Override
    public void readInfo(CommonCallback<String> callback) {
        SingleInstruction instruction = new SingleInstruction();
        instruction.setInstructionId(IdUtil.getUUID_8());
        //设置要发送的指令字符串
        //转换成十进制
        readCmd = readCmd.replace(" ", "");
        instruction.setInstruction(readCmd);
        instruction.setBeforeInstructionName(INSTRUCTION_DEFAULT_HEX);
        //默认使用com.zhdl.instruction.strategy包下后置策略
        instruction.setAfterInstructionName(ModbusAfterService.MODBUS_AFTER_SERVICE);
        //客户端通信地址, 串口为数字, 网口为ip+端口       数据来源 : port_comm 或 port_socket 表
        instruction.setPortKey(portConf.getComPortBattery());
        //设置协议  数据来源 : protocols 表
        instruction.setProtocols(ModbusAnalysis.MODBUS_ANALYSIS);
        behaviorPerform.perform(instruction);
        commonCallbackMap.put(readCmd + System.currentTimeMillis(), callback);
    }

    @Subscribe
    public void receiverInfo(AfterStrategyParam afterStrategyParam) {
        String instruction = afterStrategyParam.getInstruction();
        instruction = instruction.replaceAll(" ", "");
        instruction = instruction.toLowerCase();
        String cmdSend = readCmd;
        cmdSend = cmdSend.replaceAll(" ", "");
        cmdSend = cmdSend.toLowerCase();
        if (instruction.equals(cmdSend)){
            List<ModbusDto> list = afterStrategyParam.getNewData();
            ModbusDto modbusDto = list.get(0);
            for (String cmd : commonCallbackMap.keySet()) {
                String mapCmd = cmd;
                cmd = cmd.replaceAll(" ","");
                cmd = cmd.toLowerCase();
                if (cmd.contains(instruction)) {
                    String hexData = HexUtil.encodeHexStr(modbusDto.getUnitBuff());
                    commonCallbackMap.get(mapCmd).statusCallback(hexData);
                    log.info("是否读取当前设备--->" + hexData );
                    commonCallbackMap.remove(mapCmd);
                }
            }
        }
    }
}
